package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageRequestDTO;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


    /*
     * 分页查询
     * */
    @Override
    public PageResponseResult list(SensitiveDTO sensitiveDTO) {
        //1. 检查参数
        sensitiveDTO.checkParam();
        //2. 根据名称模糊查询
        Page pageParam = new Page<>(sensitiveDTO.getPage(), sensitiveDTO.getSize());

        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();

        if (StringUtils.isNotBlank(sensitiveDTO.getName())) {
            queryWrapper.like(AdSensitive::getSensitives, sensitiveDTO.getName());
        }
        //3. 执行操作
        IPage pageResult = this.page(pageParam, queryWrapper);

        return new PageResponseResult(sensitiveDTO.getPage(),sensitiveDTO.getSize(),pageResult.getTotal(),pageResult.getRecords());
    }

    /*
    * 新增敏感词汇
    * */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.校验
        if(adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }

        //2.查看长度不能超10
        if(adSensitive.getSensitives().length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词长度不能超过10");
        }

       int count= this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives,adSensitive.getSensitives()));
        if (count >0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已经存在");
        }
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        return ResponseResult.okResult();
    }

    /*
    * 编辑
    * */
    @Override
    public ResponseResult update(AdSensitive sensitive) {
        if(sensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"无效参数");
        }
        updateById(sensitive);
        return ResponseResult.okResult();
    }

    /*
     * 删除
     * */
    @Override
    public ResponseResult delete(Integer id) {
       //查看id
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdSensitive sensitive = getById(id);
        if(sensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

/*
* 查敏感词
* */
    @Autowired
    private AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }

}



